<template>
<view class="device">
	<view style="width: 100%; height: 660upx; overflow: hidden; position: relative;">
		<image src="/static/device.png" style="width: 100%; height: 868upx; position: absolute; left: 0; top: -110upx;"></image>		
	</view>
	<view style="width: 100%; height: auto; min-height: calc( 100% - 630upx ); background: #fff; border-radius: 30upx 30upx 0upx 0upx; position: relative;
		box-shadow: 0upx 0upx 17upx 1upx rgba(172, 172, 172, 0.22); margin-top: -30upx; box-sizing: border-box; padding: 30upx;" v-if="info">
		<view style="font-size: 32upx; color: #000000;">
			<text>{{ info.shopName || '暂无' }}</text>
			<text style="float: right; color: #ef7738;">{{info.chargeMoney}}</text>
		</view>
		<view style="font-size: 24upx; color: #ef7738; line-height: 1.5; margin-top: 16upx;" v-if="isFreeUser">今日剩余免费时长{{ todayFreeHour }}小时</view>
		<view style="font-size: 24upx; color: #58585a; line-height: 1.5;">借租{{info.freeTime}}分钟内免费，不足1小时按1小时计费</view>
		<view style="font-size: 24upx; color: #58585a; line-height: 1.5;">日封顶{{info.highest}}元，总封顶{{info.highestSum}}元</view>
		<view style="font-size: 24upx; color: #58585a; line-height: 1.5;">押金{{info.highestSum}}元，支持免押服务</view>
		<view style="width: 100%; height: 2upx; background: #cdcdcd; margin-top: 20upx;"></view>
		<view style="font-size: 32upx; color: #000000; line-height: 1.5; margin-top: 14upx;">适配接口</view>
		<image src="/static/devicetype.png" style="width: 100%; height: 92upx; margin-top: 20upx;"></image>		
		<view v-if="user.phone" style="width: 100%; height: 100upx; background-image: linear-gradient(209deg, #ef6e2a 0%, #ed7739 50%, #ea7f48 100%), linear-gradient(#000000, #000000);
			box-shadow: 0upx 3upx 17upx 1upx rgba(239, 146, 98, 0.33); border-radius: 50upx; color: #fff; text-align: center; display: flex; flex-direction: column;
			justify-content: space-around; align-items: center; margin-top: 20upx;" @click="querenzujie">
			<view style="font-size: 36upx;">确认租借</view>
		</view>
		<button v-else lang="zh_CN" open-type="getAuthorize" @getAuthorize="authPhone" style="width: 100%; height: 100upx; background-image: linear-gradient(209deg, #ef6e2a 0%, #ed7739 50%, #ea7f48 100%), linear-gradient(#000000, #000000);
			box-shadow: 0upx 3upx 17upx 1upx rgba(239, 146, 98, 0.33); border-radius: 50upx; color: #fff; text-align: center; display: flex; flex-direction: column;
			justify-content: space-around; align-items: center; margin-top: 20upx;" scope='phoneNumber' class="phoneBtn">绑定手机号后租借</button>		
	</view>
</view>
</template>

<script>
import { HTTP } from '../../http.js';
let http = new HTTP;
export default {
	data() {
		return {
			info: null,
			shebeiId: '',
			ifYajin: false,
			isFreeUser: false,
			todayFreeHour: 0,
			user: {}
		}
	},
	methods: {
		authPhone() {
			my.getPhoneNumber({
				success: (res) => {
					let encryptedData = res.response;
					http.request({
						url: `login/decodeZfbPhone?json=${ encodeURIComponent( encryptedData ) }`,
						method: 'get',
						success: (res) => {
							let json = JSON.parse( res.dataList );
							this.user.phone = json.mobile;
							uni.setStorage({ key: 'user', data: this.user });
						}
					});	
				}
			});
		},
		querenzujie(){	
			uni.showLoading({ title: '加载中', mask: true });
			if(this.ifYajin){				
				http.request({
					url: `borrowV2?id=${this.shebeiId}`,
					method: 'post',
					success: (res)=>{					
						uni.hideLoading();			
						uni.showModal({ 
							title: '提示', content: '租借成功', 
							showCancel: false,
							complete: () => {
								uni.navigateBack({ delta: 1 });
							}
						});
					},
					fail: (err)=>{			
						uni.hideLoading();
						console.log(err);	
						if(err.errorCode==3) uni.navigateTo({ url: 'yajin', success: ()=>{
							setTimeout(()=>{								
								uni.showToast({ icon: 'none', title: '请先缴纳押金' })
							},500)
						} });						
					}
				});			
			}else{
				http.request({
					url: `zfbV2/freeze?id=${this.shebeiId}`,
					method: 'post',
					success: (res)=>{		
						console.log(res);
						uni.hideLoading();
						let data=res.dataList||'';
						// data=decodeURIComponent(data);
						if(!data){ uni.showToast({ icon: 'none', title: '订单创建失败，请稍后重试！' }); return ; } 
						my.tradePay({
							orderStr: data, //完整的支付参数拼接成的字符串，从服务端获取
							success: (res) => {
								console.log(res);
								try{
									if(res.resultCode==9000){
										let json=JSON.parse(res.result);
										if(json.alipay_fund_auth_order_app_freeze_response.code==10000){
											uni.showModal({ 
												title: '提示', content: '租借成功', 
												showCancel: false,
												complete: () => {
													uni.navigateBack({ delta: 1 });
												}
											});
										}else{
											throw res;
										}
									}else{
										throw res;
									}									
								}catch(err){
									console.log(err,'失败原因');
									uni.showModal({
										title: '提示',
										content: '免押金签约失败，前往缴纳押金？',
										success: (res)=>{
											if(res.confirm){
												uni.navigateTo({ url: 'yajin' });
											}
										}
									});	
								}
								// uni.navigateBack({ delta: 1, success: ()=>uni.showToast({ icon: 'none', title: '租借成功' }) });								
							},
							fail: (err) => {
								uni.showModal({
									title: '提示',
									content: '免押金签约失败，前往缴纳押金？',
									success: (res)=>{
										if(res.confirm){
											uni.navigateTo({ url: 'yajin' });
										}
									}
								});			
							}
						});
					},
					fail: (err)=>{		
						uni.hideLoading();	
					}
				});					
			}
		},
		getFreetime(user) {
			http.request({
				url: `SpecialUserController/findUserFreeTime?sn=${ this.shebeiId }&userId=${ user.userId }`,
				method: 'get',
				success: (res) => {	
					console.log(res, 'resinfo')
					this.isFreeUser = res.dataList.isFreeUser;
					this.todayFreeHour = res.dataList.todayFreeHour;
				}
			});
		}
	},
	onLoad(e) {
		this.shebeiId=e.id;
		http.request({
			url: `borrow/myWallet?sn=${this.shebeiId}`,
			method: 'get',
			success: (res)=>{		
				console.log(res);
				let info=res.dataList||{};
				if(info.deposit>0||info.zfbCreditSwitch!='open'){ this.ifYajin=true; }
			}
		});
		http.request({
			url: `chargerPlaEquipmentController/getSNEquipmentStatus?sn=${this.shebeiId}`,
			method: 'get',
			success: (res)=>{		
				console.log(res, '设备信息');
				let info=res.dataList||{};
				if(info.ifOnline!=0) { 
					uni.showModal({
						title: '提示',
						content: '该设备正处于离线状态，请尝试其它设备或稍后再次尝试。',
						success: (res)=>{
							uni.navigateBack({ delta: 1 });
						}
					});
				}else if(info.ifBorrow!=0){ 
					uni.showModal({
						title: '提示',
						content: '该设备暂时不可借用，请尝试其它设备或稍后再次尝试。',
						success: (res)=>{
							uni.navigateBack({ delta: 1 });
						}
					});
				}else if(info.ifFault!=0){ 
					uni.showModal({
						title: '提示',
						content: '该设备处于故障中，请尝试其它设备。',
						success: (res)=>{
							uni.navigateBack({ delta: 1 });
						}
					});
				}
				this.info=info;
			}
		});
		let user = uni.getStorageSync('user');
		this.user = JSON.parse(JSON.stringify(user));
		this.getFreetime(user);
	},
}
</script>

<style>
.device {
	height: 100vh;
	position: relative;
	background: #fafafa;
	overflow: auto;
}
</style>